/**********************************************************************************
Program:       Real-time Weather Report

License:       GNU/GPL version 3

File:          CityID_Database.cs

Description:   读取 ID.dat 中的文本数据

Author:        Guangyu Liu （lgy0417@gmail.com）

Environment:   .NET Framework v2.0 or later version

Usage/Example: README_chs.txt

Last Modified: 2010.08.29

***********************************************************************************/


using System;
using System.IO;
using System.Resources;
using System.Reflection;


namespace WeatherInfo
{
	[Serializable]
	public struct Place
	{
		public string Name;
		// 此变量为当前区划的名称

		
		public int Value;
		// 如果 Value < 100000000，则 Value 表示下一级区划在二维数组中的行位置
		// 如果 Value > 100000000，表示已经到达最基层区划，此时 Value = cityID
		// 第 35 行左右为界，之前 Value < 100000000，之后反之
		
		public Place(string name, int value)
		{
			this.Name = name;
			this.Value = value;
		}
	}
	
	
	public class ID_Database
	{
		private const int _dataSize = 399;
		private const string _dataFilePath = @"ID.dat";
		private Place[][] _places;
		
		public ID_Database()
		{
			_places = new Place[_dataSize][];
		}
		
		public void ReadData()
		{
			// 读入数据，在点击编辑按钮时执行
			ResourceManager rm = new ResourceManager("ChinaWeatherGadget", Assembly.GetExecutingAssembly());
			string line;
			for (int lineNum = 0; lineNum < 399; lineNum++)
			{
				line = rm.GetString("DataLine" + lineNum.ToString());
				string[] structs = line.Split(new char[] {','});
				_places[lineNum] = new Place[structs.Length];
				for(int i = 0; i < structs.Length; i++)
				{
					string[] param = structs[i].Split(new char[] {' '});
					_places[lineNum][i] = new Place(param[0], Convert.ToInt32(param[1]));
				}
			}
		}
		
		#region 三个重载的方法，用于填充各级combox
		
		public string[] GetNameRange()
		{
			// 返回长度34的包含省名的数组，用于填充一级combobox
			string[] provinces = new string[_places[0].Length];
			for(int i = 0; i < _places[0].Length; i++)
				provinces[i] = _places[0][i].Name;
			return provinces;
		}
		
		public string[] GetNameRange(int indexOfSelectedProvince)
		{
			// 返回某省包含地级市名称的数组，用于填充二级combobox
			int citiesLineIndex = _places[0][indexOfSelectedProvince].Value;   // 某省地级市数组的行坐标
			int length = _places[citiesLineIndex].Length;   // 某省下辖城市的个数
			string[] cities = new string[length];
			for(int i = 0; i < cities.Length; i++)
				cities[i] = _places[citiesLineIndex][i].Name;
			return cities;
		}
		
		public string[] GetNameRange(int indexOfSelectedProvince, int indexOfSelectedCity)
		{
			// 返回某省包含地级市名称的数组，用于填充三级combobox
			int citiesLineIndex = _places[0][indexOfSelectedProvince].Value;  // 某省地级市数组的行坐标
			int townsLineIndex = _places[citiesLineIndex][indexOfSelectedCity].Value;  // 某省某市的县数组的行坐标
			int length = _places[townsLineIndex].Length;  // 该市下辖县的个数
			string[] towns = new string[length];
			for(int i = 0; i < towns.Length; i++)
				towns[i] = _places[townsLineIndex][i].Name;
			return towns;
		}
		
		#endregion
		
		public string GetTownFullName(SingleCity s)
		{
			// 返回选定县级区划的完整（省 - 市 - 县）名称，用于填充 ListBox
			int citiesLineIndex = _places[0][s.IndexOfProvince].Value;  // 某省地级市数组的行坐标
			string pName = _places[0][s.IndexOfProvince].Name;
			string cName = _places[citiesLineIndex][s.IndexOfCity].Name;
			return pName + " - " + cName + " - " + s.Name;
		}
		
		public Place GetTownPlaceStruct(int indexOfSelectedProvince, int indexOfSelectedCity, int indexOfSelectedTown)
		{
			int citiesLineIndex = _places[0][indexOfSelectedProvince].Value;  // 某省地级市数组的行坐标
			int townsLineIndex = _places[citiesLineIndex][indexOfSelectedCity].Value;  // 某省某市的县数组的行坐标
			return _places[townsLineIndex][indexOfSelectedTown];
		}

		
		/* 临时的Main()方法，用于测试本文件中的代码 */
		// static void Main()
		// {
			// TownID ti = new TownID();
			// DateTime m = DateTime.Now;
			// ti.ReadData();
			
			// string[] p = ti.GetNameRange();   // 返回34个省区划列表
			// foreach(string i in p)
				// Console.Write(i + " ");
			// Console.Write("\n\n");
			
			// string[] c = ti.GetNameRange(9);  // 返回河北地级市列表
			// foreach(string i in c)
				// Console.Write(i + " ");
			// Console.Write("\n\n");
			
			// string[] t = ti.GetNameRange(9, 8);  // 返回河北唐山下辖县的列表
			// foreach(string i in t)
				// Console.Write(i + " ");
			// Console.Write("\n\n");
			
			// int id = ti.GetTownID(9, 8, 5);  // 返回河北唐山滦县的ID
			// Console.Write(id.ToString() + "\n\n");
			
			// DateTime n = DateTime.Now;
			// Console.WriteLine("Time Elapsed: {0} ms", n.Subtract(m).TotalMilliseconds);
		// }
	}
}